package com.ruoyi.system.service.impl;

import java.util.List;

import com.ruoyi.common.annotation.DataScope;
import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.DevelopmentTop;
import com.ruoyi.system.mapper.SysDeptMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.ActivityFileMapper;
import com.ruoyi.system.domain.ActivityFile;
import com.ruoyi.system.service.IActivityFileService;

/**
 * 过程管控照片Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-08-06
 */
@Service
public class ActivityFileServiceImpl implements IActivityFileService 
{
    private static final Logger log = LoggerFactory.getLogger(DevelopmentTop.class);
    @Autowired
    private ActivityFileMapper activityFileMapper;
    @Autowired
    private SysDeptMapper sysDeptMapper;

    /**
     * 查询过程管控照片
     * 
     * @param id 过程管控照片主键
     * @return 过程管控照片
     */
    @Override
    public ActivityFile selectActivityFileById(Long id)
    {
        return activityFileMapper.selectActivityFileById(id);
    }

    /**
     * 查询过程管控照片列表
     * 
     * @param activityFile 过程管控照片
     * @return 过程管控照片
     */
    @Override
    public List<ActivityFile> selectActivityFileList(ActivityFile activityFile)
    {
        return activityFileMapper.selectActivityFileList(activityFile);
    }
    @Override
//    @DataScope(deptAlias = "d")
    public List<ActivityFile> selectActivityFileListNew(ActivityFile activityFile)
    {
        return activityFileMapper.selectActivityFileListNew(activityFile);
    }

    /**
     * 新增过程管控照片
     * 
     * @param activityFile 过程管控照片
     * @return 结果
     */
    @Override
    public int insertActivityFile(ActivityFile activityFile)
    {
        return activityFileMapper.insertActivityFile(activityFile);
    }

    /**
     * 修改过程管控照片
     * 
     * @param activityFile 过程管控照片
     * @return 结果
     */
    @Override
    public int updateActivityFile(ActivityFile activityFile)
    {
        return activityFileMapper.updateActivityFile(activityFile);
    }

    /**
     * 批量删除过程管控照片
     * 
     * @param ids 需要删除的过程管控照片主键
     * @return 结果
     */
    @Override
    public int deleteActivityFileByIds(Long[] ids)
    {
        return activityFileMapper.deleteActivityFileByIds(ids);
    }

    /**
     * 删除过程管控照片信息
     * 
     * @param id 过程管控照片主键
     * @return 结果
     */
    @Override
    public int deleteActivityFileById(Long id)
    {
        return activityFileMapper.deleteActivityFileById(id);
    }

    /**
     * 导入数据
     */
    @Override
    public String importActivityFile(List<ActivityFile> activityFileList, Boolean isUpdateSupport, String operName)
    {
        if (StringUtils.isNull(activityFileList) || activityFileList.size() == 0)
        {
            throw new ServiceException("导入数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (ActivityFile activityFile : activityFileList)
        {
            try
            {
                // 验证是否存在这个部门
                SysDept sysDepts = sysDeptMapper.selectDeptOnName(activityFile.getDeptName());
                if (StringUtils.isNotNull(sysDepts))
                {
                    activityFile.setCreateBy(operName);
                    activityFile.setDeptId(sysDepts.getDeptId());
                    activityFileMapper.insertActivityFile(activityFile);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、部门 " + activityFile.getDeptName() + " 过程管控数据导入成功");
                }
                else
                {
                    failureNum++;
                    failureMsg.append("<br/>" + failureNum + "、部门 " + activityFile.getDeptName() + " 不存在");
                }
            }
            catch (Exception e)
            {
                failureNum++;
                String msg = "<br/>" + failureNum + "、部门 " + activityFile.getDeptName() + " 过程管控数据导入失败：";
                failureMsg.append(msg + e.getMessage());
                log.error(msg, e);
            }
        }
        if (failureNum > 0)
        {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new ServiceException(failureMsg.toString());
        }
        else
        {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }
}
